package com.fernfx.xingtan.contacts.presenter;

import android.text.TextUtils;
import android.view.View;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.contacts.contract.AddFriendsContract;
import com.fernfx.xingtan.contacts.entity.FindUserEntity;
import com.fernfx.xingtan.contacts.model.AddFriendsModel;
import com.fernfx.xingtan.contacts.ui.AddFriendsActivity;
import com.fernfx.xingtan.contacts.ui.SubmitFriendsAskActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/7.
 */

public class AddFriendsPresenter<P extends AddFriendsContract.View> implements AddFriendsContract.Presenter{
    private P P;
    private AddFriendsContract.Model model;
    private AddFriendsActivity addFriendsActivity;

    @Override
    public void init(BaseView P) {
        this.P = (P)P;
        this.model = new AddFriendsModel();
        this.addFriendsActivity = (AddFriendsActivity)P.getActivity();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        this.P.getActivity().showLoading();
        model.request(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                P.getActivity().hideLoading();
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(),500);onNetworkError(result.getMessage(),500);
                    return;
                }
                final FindUserEntity findUserEntity = FastJsonUtil.fromBean(message,FindUserEntity.class);
                if (OtherUtil.checkRequestStatus(findUserEntity.getSuccess(),findUserEntity.getStatus())) {
                    if (null == findUserEntity.getObj()) {
                        addFriendsActivity.setPointVisibility(View.VISIBLE);
                        return;
                    }
                    addFriendsActivity.setPointVisibility(View.GONE);
                    //关闭当前页面
                    P.getActivity().finish();
                    //关闭软键盘
                    SoftInputUtil.hideSoftInput(P.getActivity());
                    //跳转到添加好友页面
                    SubmitFriendsAskActivity.start(findUserEntity.getObj().getUserId(),P.getActivity());
                }else {
                    addFriendsActivity.setPointVisibility(View.GONE);
                    ToastUtil.showCentertoast(findUserEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage,errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
    }
}
